package com.atguigu.gmall.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.entity.*;
import com.atguigu.gmall.service.*;
import com.atguigu.gmall.result.RetVal;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/product")
public class CategoryController {
    @Autowired
    private BaseCategory1Service category1Service;
    @Autowired
    private BaseCategory2Service category2Service;
    @Autowired
    private BaseCategory3Service category3Service;
    @Autowired
    private BaseCategoryViewService baseCategoryViewService;

    // 获取一级分类数据
    @GetMapping("getCategory1")
    public RetVal getCategory1(){
        List<BaseCategory1> category1List = category1Service.list(null);
        return RetVal.ok(category1List);
    }
    // 获取二级分类数据
    @GetMapping("getCategory2/{category1Id}")
    public RetVal getCategory2(@PathVariable Long category1Id){
        QueryWrapper<BaseCategory2> wrapper = new QueryWrapper<BaseCategory2>();
        wrapper.eq("category1_id", category1Id);
        List<BaseCategory2> category2List = category2Service.list(wrapper);
        return RetVal.ok(category2List);
    }

    // 获取三级分类数据
    @GetMapping("getCategory3/{category2Id}")
    public RetVal getCategory3(@PathVariable Long category2Id){
        QueryWrapper<BaseCategory3> wrapper = new QueryWrapper<BaseCategory3>();
        wrapper.eq("category2_id", category2Id);
        List<BaseCategory3> category3List = category3Service.list(wrapper);
        return RetVal.ok(category3List);
    }
    //获取首页分类数据
    @GetMapping("/getIndexCategoryInfo")
    public RetVal getIndexCategoryInfo(){
        List<JSONObject> list = baseCategoryViewService.getIndexCategoryInfo();
        return RetVal.ok(list);
    }
    @GetMapping("getCategory3ForForeign/{category3Id}")
    public BaseCategoryView getCategory3ForForeign(@PathVariable Long category3Id){
        return baseCategoryViewService.getById(category3Id);
    }

}
